function menu() {
    this.callSuper();
    this.configuration = {
        "i18n": "nls/menu",
        "apis": "../../config/apis/menu"
    };
    this._viewIndex = "menu";

    // global variables
    this.isBrowser = false;
    this.account = "";
    this.secureToken = "";
}

//Dave
function getUrlValueByName(paramName){
    var paramValue="";
    var urlthis=location.href;
    urlthis=urlthis.substring(urlthis.indexOf('?')+1,urlthis.length);
    var arr=urlthis.split('&');
    if(arr!=null){
        for(var i=0;i<arr.length;i++){
            var parmNameValue=arr[i].split('=');
            if(parmNameValue!=null&&parmNameValue.length==2){
                if(parmNameValue[0]==paramName){
                    paramValue=parmNameValue[1];
                    break;
                }
            }
        }
    }
    return paramValue;
}


menu.prototype.menuHasParsed = function() {
    var self = this,
        windowHeight = window.innerHeight;

    // event binding
    iData.get("btnAbout").click(function() {
	/* #red app menu implement start*/
        $('.push_nees_opacity').css('min-height',$(window).height());
        $('#panel_menu').css('min-height',$(window).height()).css('display','block');
        $('#push_ul').css('min-height',$(window).height());
        $('#auto_height').css('min-height',$(window).height()-469);
		require(["../../../lib/js/jquery.mobile-1.4.4.min"], function(){
			$('.ui-panel-inner').css({'padding':'0px'});
			
			$('ul.ui-listview').on('click', function(e) {
				var id = $(e.target).closest('li').attr('id');
				if(id == "push_laisee"){
					$( "#panel_menu" ).panel( "close" );
				}else if(id){ 
					console.log('==================================goTo Menu: '+id+'==============================');
					location.href = "hsbc://" + urlEncode("function=GoToSCA&id="+id);
				}
			});
			
			$( "#panel_menu" ).panel( "open" );
		 });
	/* #red app menu implement end*/
    });

    iData.get('btnAboutIt').click(function(){
        console.log('btnAboutIt...');
        self.goTo("about");
        $('#about').removeClass('hidden');
        $('#menu').addClass('hidden');
    });
    iData.get('btnAboutBack').click(function(){
        $('#menu').removeClass('hidden');
        $('#about').addClass('hidden');
    });
	
    iData.get("btnLogoff").click(function() {
        self.goTo("logoff");
    });
    //Dave
    var clickButtonState='0';
    var deliver_url='file:///web/postlogon/deliver/deliver.html';
    var dashboard_url='file:///web/postlogon/dashboard/dashboard.html';
    var shake_url='file:///web/postlogon/shake/shake.html';
    var myLaiSee_url='file:///web/postlogon/myLaisee/myLaiSee.html';
    clickButtonState=getUrlValueByName("state");

    // menu buttons
    self.registerSwipeGesture("sub-page", function() {
        if (!self.secureToken) {
            self.goTo(shake_url, true);
        }
    });
    iData.get("btnDeliver").click(function() {
        clickButtonState='1';
        $(iData.get("btnDeliver")).removeClass('ui-btn-deliver').addClass('ui-btn-deliver-sel');
        $(iData.get("btnCollect")).removeClass('ui-btn-collect-sel').addClass('ui-btn-collect');
    });
    iData.get("btnCollect").click(function() {
        clickButtonState='0';
        $(iData.get("btnCollect")).removeClass('ui-btn-collect').addClass('ui-btn-collect-sel');
        $(iData.get("btnDeliver")).removeClass('ui-btn-deliver-sel').addClass('ui-btn-deliver');
    });

    if(clickButtonState=='1'){
        $(iData.get("btnDeliver")).removeClass('ui-btn-deliver').addClass('ui-btn-deliver-sel');
        $(iData.get("btnCollect")).removeClass('ui-btn-collect-sel').addClass('ui-btn-collect');
    }else{
        $(iData.get("btnCollect")).removeClass('ui-btn-collect').addClass('ui-btn-collect-sel');
        $(iData.get("btnDeliver")).removeClass('ui-btn-deliver-sel').addClass('ui-btn-deliver');
    }

    iData.get("btnShake").click(function() {
        if(clickButtonState=='1'){
            self.goTo(deliver_url+'?state=1', true);
        }else{
            self.goTo(shake_url+'?state=0', true);
        }
    });
    iData.get("btnBuzz").click(function() {
        location.href = "hsbc://function=SoundWaveToNative";
    });
    iData.get("btnMylaisees").click(function() {
        //Dave
        if(clickButtonState=='1'){
            self.goTo(dashboard_url+'?state=1', true);
        }else{
            self.goTo(myLaiSee_url+'?state=0',true);
        }
    });

    // get customerData from local db
    self.getCustomerInfo(self.getCustomerInfoCallback);
};

menu.prototype.getCustomerInfoCallback = function(customerData) {
    var self = this,
        keyNames = self.getCustomerInfoKeys();
	
	/*Temp fix for secure Token*/
	customerData[keyNames.secureToken] = "d0a86970cfb52b2e2f4749943ee990b3e941a26a9bff998ef564ed9d2a8d5006";
	self.setCustomerInfo(customerData,function(){});
	/*Temp fix for secure Token*/
    if (self.isBrowser) {
        //Dave
//        customerData = {"account": "12345678", "fullName": "Nick X L AO", "location": "CN"};
          customerData = {"account": "12345678", "fullName": "Nick X L AO", "secureToken": "12345", "location": "CN"};

    }
    // save customer info to variables
    self.account = customerData[keyNames.account];
    self.secureToken = customerData[keyNames.secureToken];
    // update the layout
    iData.get("fullName").text(customerData[keyNames.fullName]);
	$("#push_username").html(customerData[keyNames.fullName]+' !');
    // boss
    if (self.secureToken) {
        iData.get("swipeShake").addClass("hidden");
        iData.get("bossMenu").removeClass("hidden");
    // staff
    } else {
        iData.get("bossMenu").removeClass("hidden");
        $('#btnDeliver').hide();
        $('#btnCollect').hide();
    }

    // load    //update of Dave
//    self.getter("deliver", function(data) {
//        if (data == true) {
//            $(iData.get("btnDeliver")).removeClass('ui-btn-deliver').addClass('ui-btn-deliver-sel');
//            $(iData.get("btnCollect")).removeClass('ui-btn-collect-sel').addClass('ui-btn-collect');
//            iData.get("btnShake").attr('link', 'file:///web/postlogon/deliver/deliver.html');
//        } else {
//            $(iData.get("btnCollect")).removeClass('ui-btn-collect').addClass('ui-btn-collect-sel');
//            $(iData.get("btnDeliver")).removeClass('ui-btn-deliver-sel').addClass('ui-btn-deliver');
//            iData.get("btnShake").attr('link', 'file:///web/postlogon/shake/shake.html');
//        }
//    });
};

menu.prototype.registerSwipeGesture = function(element, callback) {
    var self = this,
        startPos, endPos, point,
        supportTouch = "ontouchstart" in window,
        START_EV = "mousedown", MOVE_EV = "mousemove", END_EV = "mouseup mouseleave",
        isStart = false;

    // check if support touch event
    if (supportTouch) {
        START_EV = "touchstart";
        MOVE_EV = "touchmove";
        END_EV = "touchend touchcancel";
    }

    // convert to jQuery object if "element" is object id instead of object
    if (typeof element === "string") {
        element = $("#" + element);
    } else if (typeof element === "object" && element.length === undefined) {
        element = $(element);
    }

    if (element.length > 0) {
        element.on(START_EV, function(e) {
            isStart = true;
            point = e.originalEvent.touches ? e.originalEvent.touches[0] : e;
            startPos = point.pageX;
        // move the switch button when mouse or touch moving
        }).on(MOVE_EV, function(e) {
            e.preventDefault();
            if (isStart) {
                point = e.originalEvent.touches ? e.originalEvent.touches[0] : e;
                endPos = point.pageX;
            }
        // event fired when mouse move out of the target element
        // or too many touch points
        }).on(END_EV, function(e) {
            if (isStart) {
                isStart = false;
                if (endPos - startPos < 0) {
                    callback.call(self);
                }
            }
        });
    }
};

menu.prototype.logoffHasParsed = function() {
    var self = this;

    // event binding
    iData.get("btnOKLogoff").click(function() {
        self.request({
            name: "logoffAccount",
            method: "POST",
            data: self.logoffRequestParam,
            callback: self.logoffCallback
        });
    });
    iData.get("btnCancel").click(function() {
        self.goBack("menu");
    });
};

menu.prototype.logoffRequestParam = function() {
    var self = this;

    return {
        account: self.account,
        secureToken: self.secureToken
    };
};

menu.prototype.logoffCallback = function() {
    var self = this;

    return {
        "success": function(respData) {
            self.setCustomerInfo("", function() {
                // clean up game settings as well
                self.setter("setting", "", function() {
                    self.goBack("file:///web/prelogon/logon/logon.html", true);
                });
            });
        },
        "error": function(respData) {
            // DO NOTHING
        }
    };
};

menu.prototype.aboutHasParsed = function() {
    var self = this;

    // event binding
    iData.get("btnAboutBack").click(function() {
        self.goBack("menu");
    });
};

menu.prototype.buildModel = function() {
    var self = this,
        GLOBAL_CONFIG = self.configuration.settings;

    // for browser mode
    self.isBrowser = GLOBAL_CONFIG.isBrowser;

    iData.add({
        "logo": $(".ui-logo-green-laisee"),
        // landing
        "btnAbout": $("#btnAbout"),
        "btnLogoff": $("#btnLogoff"),
        "fullName": $("#fullName"),
        "swipeShake": $("#swipeShake"),
        "bossMenu": $("#bossMenu"),
        "btnDeliver": $("#btnDeliver"),
        "btnCollect": $("#btnCollect"),
        // logOff
        "btnOKLogoff": $("#btnOKLogoff"),
        "btnCancel": $("#btnCancel"),
        // about
        "btnAboutBack": $("#btnAboutBack"),
        // menu shake buzz laisees
        "btnAboutIt": $("#btnAboutIt"),
        "btnShake": $("#btnShake"),
        "btnBuzz": $("#btnBuzz"),
        "btnMylaisees": $("#btnMylaisees")
    });
};

menu.superclass = iFunction;
menu.start = function() {
    return new menu();
};
iClass(menu);